package com.shop.loan.proxy;

import cn.hutool.core.date.DateUtil;
import com.shop.loan.mapper.LoanOrderBillByMybatisMapper;
import com.shop.loan.model.entity.LoanOrderBillByMybatis;
import com.shop.loan.model.entity.LoanOrderByMybatis;
import com.shop.loan.model.view.StatisticsByOrderNum;
import com.shop.loan.util.Util;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import xyz.erupt.annotation.fun.DataProxy;
import xyz.erupt.annotation.query.Condition;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class StatisticsByOrderNumProxy implements DataProxy<StatisticsByOrderNum> {


    @Resource
    Util util;

    @Resource
    LoanOrderBillByMybatisMapper loanOrderBillByMybatisMapper;

    @Override
    public String beforeFetch(List<Condition> conditions) {
        conditions.clear();
        return null;
    }

    @Override
    public void afterFetch(Collection<Map<String, Object>> list) {
        String channelIdStr = util.getSearchParam("channel", 1);
        String isExpireStr = util.getSearchParam("isExpire", 1);
        Long channelId = StringUtils.hasLength(channelIdStr) ? Long.valueOf(channelIdStr) : null;
        for (Map<String, Object> stringObjectMap : list) {
            Integer number = Integer.valueOf(stringObjectMap.get("number").toString());

            LoanOrderByMybatis loanOrderByMybatis = new LoanOrderByMybatis();
            loanOrderByMybatis.setChannelId(channelId);
            if (number > 0) {
                loanOrderByMybatis.setOrderNum(number);
            }
            if (StringUtils.hasLength(isExpireStr)) {
                loanOrderByMybatis.setEndTime(DateUtil.endOfDay(new Date()).toString());
            }
            Integer orderNum = loanOrderBillByMybatisMapper.selectOrderNumCount(loanOrderByMybatis);
            orderNum = orderNum == null ? 0 : orderNum;
            LoanOrderBillByMybatis billSum = loanOrderBillByMybatisMapper.selectSum(loanOrderByMybatis);
            if (billSum == null || billSum.getMoney() == null) {
                billSum = new LoanOrderBillByMybatis();
                billSum.setMoney(BigDecimal.ZERO);
                billSum.setPayMoney(BigDecimal.ZERO);
                billSum.setDiscountsMoney(BigDecimal.ZERO);
                billSum.setRateMoney(BigDecimal.ZERO);
                billSum.setOverdueFine(BigDecimal.ZERO);
            }
            //逾期金额
            loanOrderByMybatis.setPayStatus(0);

            BigDecimal overdueMoney = loanOrderBillByMybatisMapper.selectUnPayMoney(loanOrderByMybatis);
            overdueMoney = overdueMoney == null ? BigDecimal.ZERO : overdueMoney;

            BigDecimal money = billSum.getMoney().add(billSum.getRateMoney());

            BigDecimal totalPay = billSum.getPayMoney();
            BigDecimal totalDic = billSum.getDiscountsMoney();

            BigDecimal payRate = totalPay.divide(money.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ONE : money, 4, BigDecimal.ROUND_DOWN).multiply(BigDecimal.valueOf(100));
            BigDecimal overdueRate = overdueMoney.divide(money.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ONE : money, 4, BigDecimal.ROUND_DOWN).multiply(BigDecimal.valueOf(100));
            BigDecimal discountsRate = totalDic.divide(money.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ONE : money, 4, BigDecimal.ROUND_DOWN).multiply(BigDecimal.valueOf(100));

            stringObjectMap.put("orderNum", orderNum);
            stringObjectMap.put("money", money);
            stringObjectMap.put("discountsMoney", billSum.getDiscountsMoney());
//            stringObjectMap.put("discountsMoneyByGoods", billSum.getDiscountsMoneyByGoods());
            stringObjectMap.put("payMoney", billSum.getPayMoney());
//            stringObjectMap.put("payMoneyByGuarantee", billSum.getPayMoneyByGuarantee());
            stringObjectMap.put("overdueMoney", overdueMoney);
            stringObjectMap.put("overdueRate", overdueRate);
            stringObjectMap.put("discountsRate", discountsRate);
            stringObjectMap.put("payRate", payRate);
        }
    }

}
